var app = getApp();
Page({
  data: {
    NAME: '',
    STUDENT_PHONE: '',
    CONSUMER_COMPANY: '',
    index: 0, // 题目序列
    chooseValue: [], // 选择的答案序列
    totalScore: 100, // 总分
    wrongList: [], // 错误的题目集合
    testId: "" //标题
  },
  onLoad: function (options) {

    const {
      NAME
    } = options

    const {
      STUDENT_PHONE
    } = options

    const {
      CONSUMER_COMPANY
    } = options


    console.log('ren', NAME);
    console.log('ren', STUDENT_PHONE);
    console.log('ren', CONSUMER_COMPANY);

    console.log('标题', options.testId);
    wx.setNavigationBarTitle({
      title: options.testId
    }) // 动态设置导航条标题

    this.setData({


      NAME: NAME, //任命
      STUDENT_PHONE: STUDENT_PHONE, //手机号
      questionList: app.globalData.fxpgList[options.testId], // 拿到答题数据
      testId: options.testId, // 课程ID
      CONSUMER_COMPANY, //部门
    })
  },
  /*
   * 单选事件
   */
  radioChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.data.chooseValue[this.data.index] = e.detail.value;
    console.log(this.data.chooseValue);
  },
  /*
   * 多选事件
   */
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.data.chooseValue[this.data.index] = e.detail.value.sort();
    console.log(this.data.chooseValue);
  },
  /*
   * 下一题/提交 按钮
   */
  nextSubmit: function () {
    // 如果没有选择
    if (this.data.chooseValue[this.data.index] == undefined || this.data.chooseValue[this.data.index].length == 0) {
      wx.showToast({
        title: '请选择至少一个答案!',
        icon: 'none',
        duration: 2000,
        success: function () {
          return;
        }
      })
      return;
    }

    // 判断答案是否正确
    this.chooseError();

    // 判断是不是最后一题
    if (this.data.index < this.data.questionList.length - 1) {
      // 渲染下一题
      this.setData({
        index: this.data.index + 1
      })
    } else {
      // 跳转到结果页
      let wrongList = JSON.stringify(this.data.wrongList);
      let chooseValue = JSON.stringify(this.data.chooseValue);
      wx.navigateTo({
        // url: '../results/results?totalScore=' + this.data.totalScore + '&wrongList=' + wrongList + '&chooseValue=' + chooseValue
        url: `../fxpgfenshu/fxpgfenshu?totalScore=${this.data.totalScore}&wrongList=${wrongList}&chooseValue=${chooseValue}&testId=${this.data.testId}&NAME=${this.data.NAME}&STUDENT_PHONE=${this.data.STUDENT_PHONE} &CONSUMER_COMPANY=${this.data.CONSUMER_COMPANY}`
      })

      // test.js

      // 设置缓存
      var logs = wx.getStorageSync('logs') || []
      let logsList = {
        "date": Date.now(),
        "testId": this.data.testId,
        "score": this.data.totalScore
      }
      logs.unshift(logsList);
      wx.setStorageSync('logs', logs);

    }
  },
  /*
   * 错题处理
   */
  chooseError: function () {
    var trueValue = this.data.questionList[this.data.index]['true'];
    var chooseVal = this.data.chooseValue[this.data.index];
    console.log('选择了' + chooseVal + '答案是' + trueValue);
    if (chooseVal.toString() != trueValue.toString()) {
      this.data.wrongList.push(this.data.index);
      this.setData({
        totalScore: this.data.totalScore - this.data.questionList[this.data.index]['scores'] // 扣分操作
      })


    }
    console.log('得分', this.data.totalScore);
  },
  // 退出答题
  outTest() {

    wx.showModal({
      title: '温馨提示~',
      content: '退出后不会保存当前记录',
      confirmText: "确定", //只保留确定更新按钮
      success: function (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '../home/home'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }

      },

    })

  }
})